package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Store;
import cn.edu.scau.dropship.entity.StoreExample;
import cn.edu.scau.dropship.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private StoreService storeService;

    /*查看网店*/
    @PostMapping(value = "/getStoreList")
    @ResponseBody
    public List<Store> GetAllStore(HttpServletRequest request) {
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        List<Store> list=storeService.selectByUid(uid);
        return list;
    }

    /*添加店铺*/
    @PostMapping(value = "/addStore")
    @ResponseBody
    public String updateStore(HttpServletRequest request) {
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        String name=request.getParameter("name");
        String type=request.getParameter("type");

        if(name.equals("")){
            return "false";
        }

        StoreExample storeExample=new StoreExample();
        StoreExample.Criteria criteria=storeExample.createCriteria();
        criteria.andIdIsNotNull();
        List<Store> list=storeService.selectByExample(storeExample);
        for(Store store:list){
            if(name.equals(store.getName())){
                return "false";
            }
        }

        Store store=new Store();
        store.setBvoId(uid);
        store.setName(name);
        store.setType(type);
        int result=storeService.insert(store);
        if(result<=0){
            return "false";
        }
        return "true";
    }
}
